package com.fengke.cn.yejian11.activity;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.adapter.ShaiTuCommentAdapter;
import com.fengke.cn.yejian11.base.BaseActivity;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.impls.ActImpls;
import com.fengke.cn.yejian11.javabean.ShaiTuComment;
import com.fengke.cn.yejian11.javabean.ShaiTuInfo;
import com.fengke.cn.yejian11.utils.RoundImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShaiTuInfoActivity extends BaseActivity {

    private TextView S_Name;
    private ImageView S_Pic;
    private TextView S_Time;
    private TextView S_ClubName;
    private TextView S_Title;
    private ImageView S_address;
    private ImageView S_address1;
    private ImageView S_address2;
    String ShaiTu_Id;
    String related;
    private ShaiTuCommentAdapter shaiTuCommentAdapter;
    private RecyclerView recyclerView;
    private Retrofit retrofit;
    String iddd;
    private Button comment_button;
    private EditText comment_content;
    String content;
    String shai_user_id;
    String reply_id;
    String club_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shai_tu_info);

//iddd="1";
        club_name=getIntent().getStringExtra("Suser_ClubName");
        ShaiTu_Id=getIntent().getStringExtra("ShaiTu_Id");
        related="1";
        reply_id=getUserId();

//        String Suser_Name=getIntent().getStringExtra("Suser_Name");
//        String Suser_Pic=getIntent().getStringExtra("Suser_Pic");
//        String Suser_Time=getIntent().getStringExtra("Suser_Time");
//        String Suser_ClubName=getIntent().getStringExtra("Suser_ClubName");
//        String Suser_Title=getIntent().getStringExtra("Suser_Title");
//        String Suser_address=getIntent().getStringExtra("Suser_address");
        String Suser_address1=getIntent().getStringExtra("Suser_address1");
        String Suser_address2=getIntent().getStringExtra("Suser_address2");
        recyclerView=(RecyclerView)findViewById(R.id.shaitu_info_recyclerview);
        comment_button= (Button) findViewById(R.id.shaitu_info_button);
        comment_content= (EditText) findViewById(R.id.shaitu_info_content);

        S_Name= (TextView) findViewById(R.id.Suser_Name);
        S_Pic= (RoundImageView) findViewById(R.id.Suser_Pic);
        S_Time= (TextView) findViewById(R.id.Suser_Time);
        S_ClubName= (TextView) findViewById(R.id.Suser_ClubName);
        S_Title= (TextView) findViewById(R.id.Suser_Title);
        S_address= (ImageView) findViewById(R.id.Suser_address);
        S_address1= (ImageView) findViewById(R.id.Suser_address1);
        S_address2= (ImageView) findViewById(R.id.Suser_address2);
        ////////////////////////////////////////////////////////////////////
        Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
        ActImpls services=retrofit.create(ActImpls.class);
        Call<ShaiTuInfo> call=services.getShaiTuInfo(ShaiTu_Id,related);
        call.enqueue(new Callback<ShaiTuInfo>() {
            @Override
            public void onResponse(Call<ShaiTuInfo> call, Response<ShaiTuInfo> response) {
                ShaiTuInfo shaiTuInfo=response.body();
                shai_user_id=shaiTuInfo.getUserid();
                if (shaiTuInfo.getShaier()==null){
                    S_Name.setText("yinjinghua");
                    S_Pic.setImageResource(R.drawable.dog_1);
                }else {
                    S_Name.setText(shaiTuInfo.getShaier().getUser_name());
                    if (shaiTuInfo.getShaier().getPhoto()==null){
                        Picasso.with(getBaseContext()).load(R.drawable.dog_1).into(S_Pic);
                    }else {
                        Picasso.with(getBaseContext()).load(Config.PIC_URL + shaiTuInfo.getShaier().getPhoto()).into(S_Pic);
                    }
                }
                S_ClubName.setText(club_name);
                S_Time.setText(shaiTuInfo.getUpdate_time());
                if (shaiTuInfo.getTitle().equals("")){
                    S_Title.setText("今天认识了很多的朋友,乐死我了");
                }else{
                S_Title.setText(shaiTuInfo.getTitle());
                }
                if (!shaiTuInfo.getThumb0().equals("")) {
                    Picasso.with(getBaseContext()).load(Config.PIC_URL + shaiTuInfo.getThumb0()).into(S_address);
                }else {
                    Picasso.with(getBaseContext()).load(R.drawable.dog_1).into(S_address);
                }
            }

            @Override
            public void onFailure(Call<ShaiTuInfo> call, Throwable t) {

            }
        });
        ////////////////////////////////////////////////////////////////////
        Picasso.with(ShaiTuInfoActivity.this)
                .load(Suser_address1)
                .placeholder(R.drawable.dog_1)
                .noFade()
                .into(S_address1);
        S_address2= (ImageView) findViewById(R.id.Suser_address2);
        Picasso.with(ShaiTuInfoActivity.this)
                .load(Suser_address2)
                .placeholder(R.drawable.dog)
                .noFade()
                .into(S_address2);
        getComment(ShaiTu_Id);
        ///////////////////////////////////////////////////
        comment_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                content=comment_content.getText().toString();
                if (content.equals("")){
                 Toast.makeText(ShaiTuInfoActivity.this,"您输入的信息为空",Toast.LENGTH_LONG).show();
                }else {
                    Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
                    ActImpls services = retrofit.create(ActImpls.class);
                    Call<Integer> call = services.addShaiTuComment(ShaiTu_Id, shai_user_id, reply_id,content);
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {
                            if (response.body() > 0) {
                                Toast.makeText(ShaiTuInfoActivity.this, "留言成功", Toast.LENGTH_LONG).show();
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {

                        }
                    });
                }
            }
        });
        ///////////////////////////////////////////////////
    }

    public void getComment(String user_id){
        Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl(Config.URL).build();
        ActImpls services=retrofit.create(ActImpls.class);
        Call<List<ShaiTuComment>> call=services.getShaiTuComment(user_id);
        call.enqueue(new Callback<List<ShaiTuComment>>() {
            @Override
            public void onResponse(Call<List<ShaiTuComment>> call, Response<List<ShaiTuComment>> response) {
                List<ShaiTuComment> list=response.body();
                shaiTuCommentAdapter=new ShaiTuCommentAdapter(ShaiTuInfoActivity.this,list);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(ShaiTuInfoActivity.this);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(linearLayoutManager);
                recyclerView.setAdapter(shaiTuCommentAdapter);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
            }

            @Override
            public void onFailure(Call<List<ShaiTuComment>> call, Throwable t) {

                Toast.makeText(ShaiTuInfoActivity.this,"获取列表失败",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
